//
//  RequestManager.m
//  YCGRICE
//
//  Created by Liu Feng on 2018/7/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "RequestManager.h"
#import "RequestManager+GR.h"
#import "NSMutableURLRequest+GR.h"

static const char *REQUEST_METHOD[5] = {"GET", "POST", "PUT", "PATCH", "DELETE"};

@implementation RequestManager


+ (NSString *)stringWithMethod:(GRMethod)method {
    const char *methodChar = REQUEST_METHOD[method];
    return [NSString stringWithUTF8String:methodChar];
}

+ (void)TouristsRequestWithURLString:(NSString *)urlString Method:(GRMethod  )method Paramers:(NSDictionary *)paramers completionHandler:(RequestBlock)completionHandler {
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:[self stringWithMethod:method] URLString:urlString parameters:paramers error:nil];
    
    request.paramers = paramers;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [self dataTasKWithRequest:request completionHandler:completionHandler];

}


+ (void)MembershipRequestWithURLString:(NSString *)urlString Method:(GRMethod)method Paramers:(NSDictionary *)paramers completionHandler:(RequestBlock)completionHandler {
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:[self stringWithMethod:method] URLString:urlString parameters:paramers error:nil];

    request.paramers = paramers;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    NSString *token = [GRUserBasisInfo Token];
    if (token && token.length > 0) {
        [request setValue:[NSString stringWithFormat:@"Bearer %@",token] forHTTPHeaderField:@"Authorization"];
    }
        
    [self dataTasKWithRequest:request completionHandler:completionHandler];
}



+ (void)MultipartFormRequestWithURLString:(NSString *)urlString
                                   Method:(GRMethod  )method
                                 Paramers:(NSDictionary *)paramers
                        completionHandler:(RequestBlock)completionHandler {

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:[self stringWithMethod:method] URLString:urlString parameters:paramers constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSLog(@"%@",formData);
    } error:nil];
    
    request.paramers = paramers;
    [self dataTasKWithRequest:request completionHandler:completionHandler];
}



+ (void)uploadTaskWithURLString:(NSString *)urlString
                         Method:(GRMethod  )method
                  GRFileObjects:(NSArray
                                 <GRFileObject *> *)fileObjects
                       Progress:(void(^)
                                 (NSProgress *uploadProgress))uploadProgressBlock
              completionHandler:(RequestBlock)completionHandler {
    
    if (!fileObjects || fileObjects.count == 0) {
        NSString *localizedDescription = @"参数：<files> 不允许为空";
        NSDictionary *errorInfo = @{NSLocalizedDescriptionKey: localizedDescription, NSLocalizedFailureReasonErrorKey: localizedDescription};
        completionHandler(NO, nil, [GRError errorWithDomain:@"com.guiMi.yecai" code:-299 userInfo:errorInfo]);
        return;
    }
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:[self stringWithMethod:method] URLString:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (GRFileObject *fileObject in fileObjects) {
            [formData appendPartWithFileData:fileObject.data name:fileObject.name fileName:fileObject.fileName mimeType:fileObject.mimeType];
        }
        
    } error:nil];
    
    
    [self uploadWithStreamedRequest:request progress:uploadProgressBlock completionHandler:completionHandler];
}




@end
